package team.zero.lemon.module.sys.web;

import team.zero.lemon.common.authz.annotation.AuthPassport;
import team.zero.lemon.common.authz.annotation.AuthPermission;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.web.BaseController;
import team.zero.lemon.module.sys.entity.Role;
import team.zero.lemon.module.sys.entity.RoleFunction;
import team.zero.lemon.module.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by bolin on 2016/5/10.
 */
@Controller
@ResponseBody
@AuthPassport
@RequestMapping(value = "${adminPath}/sys/role", method = {RequestMethod.POST, RequestMethod.GET})
public class RoleController  extends BaseController {

    @Autowired
    private RoleService roleService;
    @AuthPassport
    @RequestMapping("/query/page")
    @AuthPermission("sys:role:view")
    public Page<Role> findRole(@RequestBody Role role) {
        return roleService.findPage(role);
    }

    @AuthPassport
    @RequestMapping("/save")
    @AuthPermission("sys:role:edit")
    public int SaveRole(@RequestBody Role role) {
        return roleService.save(role);
    }

    @AuthPassport
    @RequestMapping("/delete/ids")
    @AuthPermission("sys:role:delete")
    public int deleteRole(@RequestBody Role role) {
        return roleService.deleteLogicMulti(role);
    }

    @AuthPassport
    @RequestMapping("/query/id")
    @AuthPermission("sys:role:view")
    public List<Role> findRoleById(@RequestBody int userId) {
        return roleService.findRoleByUserId(userId);
    }

    @AuthPassport
    @RequestMapping("/query/all")
    @AuthPermission("sys:role:view")
    public List<Role> findAllRoles() {
        return roleService.findAll();
    }
    @AuthPassport
    @RequestMapping("/query/tree")
    public List<Role> findRoleTree() {
        return roleService.findRoleTree();
    }
    @AuthPassport
    @RequestMapping("/query/parameter")
    @AuthPermission("sys:role:view")
    public List<Role> findRoleByParameter(@RequestBody Role role) {
        return roleService.findByParameter(role);
    }
    @AuthPassport
    @RequestMapping("/function/query/roleId")
    @AuthPermission("sys:role:function:view")
    public List<RoleFunction> findFunctionByRoleId(@RequestBody RoleFunction roleFunction) {
        return roleService.findFunctionByRoleId(roleFunction);
    }

    @AuthPassport
    @RequestMapping("/function/save")
    @AuthPermission("sys:role:function:edit")
    public int SaveRoleFunction(@RequestBody List<RoleFunction> roleFunction) {
        return roleService.saveRoleFunction(roleFunction);
    }

    @AuthPassport
    @RequestMapping("/function/delete/roleId")
    @AuthPermission("sys:role:function:delete")
    public int roleFunctionDelete(@RequestBody List<RoleFunction> roleFunction) {
        return roleService.roleFunctionDelete(roleFunction);
    }

}
